package com.wHitE.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;

//@WebFilter(urlPatterns = {
//        "/login",
//        "/register",
//        "/bookList",
//        "/addBook",
//        "/editBook",
//        "/deleteBook"
//})
@WebFilter("/*")
public class ServletFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("------服务器端启动...------");
        System.out.println("        \n" +
                "           ┌─────────────────────────────────────────────────────────────┐\n" +
                "           │┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐│\n" +
                "           ││Esc│!1 │@2 │#3 │$4 │%5 │^6 │&7 │*8 │(9 │)0 │_- │+= │|\\ │`~ ││\n" +
                "           │├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴───┤│\n" +
                "           ││ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{[ │}] │ BS  ││\n" +
                "           │├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤│\n" +
                "           ││ Ctrl │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│\" '│ Enter  ││\n" +
                "           │├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────┬───┤│\n" +
                "           ││ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│Shift │Fn ││\n" +
                "           │└─────┬──┴┬──┴──┬┴───┴───┴───┴───┴───┴──┬┴───┴┬──┴┬─────┴───┘│\n" +
                "           │      │Fn │ Alt │         Space         │ Alt │Win│   HHKB   │\n" +
                "           │      └───┴─────┴───────────────────────┴─────┴───┘          │\n" +
                "           └─────────────────────────────────────────────────────────────┘\n" +
                "         ");
        System.out.println(" bug走开 ~ ~ \n");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        if (servletRequest.getContentType() != null && servletRequest.getContentType().equals("application/json")) {
            servletResponse.setContentType("application/json;charset=utf-8");
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("------服务器端关闭...------");
    }
}
